/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import net.groupme.model.Tag;
import net.groupme.model.TagImpl;
import net.groupme.model.dao.DBProperties;
import net.groupme.model.dao.TagDAOImpl;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;

/**
 * 
 * This class provides...
 * This interface defines...
 * 
 * @author  Mischa Frank, <a href="mailto:code@planetfrank.net">code@planetfrank.net</a>
 * 
 * @version 0.1a, created on 14.05.2007 
 *
 */
public class GetTagsQuery extends MappingSqlQuery {
	private String columnName;
	private DataSource datasource;
	
	// table
	public static final String TABLE_TAG = DBProperties
			.getString("GroupmeDB.TABLE_TAG"); //$NON-NLS-1$

	// columns
	public static final String TAG_KEYWORD = DBProperties
			.getString("GroupmeDB.TAG_KEYWORD"); //$NON-NLS-1$
	
	public static final String TAG_ID = DBProperties
	.getString("GroupmeDB.TAG_ID"); //$NON-NLS-1$
	
	public GetTagsQuery(DataSource ds, String query, String columnName) {
        super(ds, query);
        this.columnName = columnName;
        this.datasource = ds;
        compile();
    }
	

	public GetTagsQuery(DataSource ds) {
		super(ds, "SELECT " + TAG_ID + " FROM " + TABLE_TAG + " WHERE " 
				+ TAG_KEYWORD + " =?"); 
        super.declareParameter(new SqlParameter("keyword", Types.VARCHAR));
        this.columnName = 	TAG_ID;
        this.datasource = ds;
        compile();
        
    }

    protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        List<Tag> tags = new ArrayList<Tag>();
         while (!rs.isAfterLast()){
             Tag tag = new TagImpl(new TagDAOImpl(datasource, rs.getString(columnName)));
             tags.add(tag);
        	 rs.next();
        }
    	return tags; 
    }

}
